﻿using System;
using System.Collections.Generic;

using Sgs.Model;
using static Sgs.Model.Player;
using static Sgs.Mode.Identity;
using static Sgs.Utils.Method;


namespace Sgs.Core
{
    class Controller
    {
        private readonly Store Store;
        private readonly Module Module;
        private readonly List<Player> Players = new List<Player>();

        public int PlayerCount
        {
            get
            {
                return Players.Count;
            }
        }

        public enum GameOverOptions
        {
            主忠胜利,反贼胜利,内奸胜利,平局
        }

        public Store PlayAccessStore
        {
            get
            {
                return Store;
            }

            set
            {
                throw new ApplicationException("不允许Player修改Store类");
            }
        }

        public Player UnSafeTouchAnotherPlayer(int PlayerIndex)
        {
            return Players[PlayerIndex];
        }

        public Controller(PackageModule Package, GamePattern gamePattern)
        {
            //加载模块
            Console.WriteLine("初始化完成");
            Console.WriteLine();

            Module = Package.Provide();
            Store = new Store(Shuffle(Module.Cards));
            List<Hero> Heros = Shuffle(Module.Heroes);

            // 加载游戏策略
            PlayerIdentity[] Camps = gamePattern.ProvideCampList();
            for (int i = 0; i < gamePattern.PlayerCount; i++)
            {
                //一号位永远是主公
                Player player;
                if(i == 0)
                {
                    player = new Player(i, PlayerIdentity.主公, Heros[i], Store.DispatchCards(4), this);
                    player.Hero.As主公();
                }
                else
                {
                    player = new Player(i, Camps[i - 1], Heros[i], Store.DispatchCards(4), this);
                }                   
                player.SetControllerIndex(i);
                Players.Add(player);
                player.UnSafeShowDetails();
            }

            LoadPlayer();
        }

        public void GameOver(GameOverOptions options)
        {
           
            if(options == GameOverOptions.平局)
            {
                Console.WriteLine("平局：没有胜利方");
            }

            else if(options == GameOverOptions.主忠胜利)
            {
                Console.WriteLine("主忠胜利");
                string GameOverMessage = "获胜方：";
                foreach (Player player in Players)
                {
                    if (player.Identity == PlayerIdentity.主公 || player.Identity == PlayerIdentity.忠臣)
                    {
                        GameOverMessage += $"{player.Index + 1}号 ";
                    }
                }
                Console.WriteLine(GameOverMessage);
            }
            else if(options == GameOverOptions.反贼胜利)
            {
                Console.WriteLine("反贼胜利");
                string GameOverMessage = "获胜方：";
                foreach (Player player in Players)
                {
                    if (player.Identity == PlayerIdentity.反贼)
                    {
                        GameOverMessage += $"{player.Index + 1}号 ";
                    }
                }
                Console.WriteLine(GameOverMessage);
            }

            else
            {
                Console.WriteLine("内奸胜利");
                string GameOverMessage = "获胜方：";
                foreach (Player player in Players)
                {
                    if (player.Identity == PlayerIdentity.内奸)
                    {
                        GameOverMessage += $"{player.Index + 1}号 ";
                    }
                }
                Console.WriteLine(GameOverMessage);
            }

            Console.WriteLine("");
            Console.Write("游戏结束");
            Console.ReadKey();
            Environment.Exit(0);
        }

        public void LoadPlayer(int PlayerIndex = 0)
        {
            Players[PlayerIndex].StartRound();
        }

        public void RemovePlayer(int PlayerIndex)
        {
            Player TargetPlayer = GetPlayerByIndex(PlayerIndex);
            if(TargetPlayer.Identity == PlayerIdentity.主公)
            {
                if (PlayerCount == 2 && (Players[1].Identity == PlayerIdentity.内奸))
                {
                    GameOver(GameOverOptions.内奸胜利);
                } else
                {
                    GameOver(GameOverOptions.反贼胜利);
                }
            } else
            {
                int 反内数量 = 0;
                for(int i = 0; i< PlayerCount; i++)
                {
                    if(Players[i].Identity == PlayerIdentity.反贼 || Players[i].Identity == PlayerIdentity.内奸)
                    {
                        反内数量++;
                    }
                }

                if(反内数量 == 0)
                {
                    GameOver(GameOverOptions.主忠胜利);
                }
            }
            Players.RemoveAt(TargetPlayer.GetControllerIndex());
            for(int i = 0; i< PlayerCount; i++)
            {
                Players[i].SetControllerIndex(i);
            }
        }

        public Player GetPlayerByIndex(int PlayerIndex)
        {
            Player player = null;
            for (int i = 0; i<PlayerCount; i++)
            {
                if(Players[i].Index == PlayerIndex)
                {
                    player =  Players[i];
                }
            }
            return player;
        }
    }
}
